#include "swmtask.h"

SWMTask::SWMTask(QObject *parent) : QObject(parent)
{

}

/*
 *  获取任务模式
 */
int SWMTask::getModel(int id, int seq)
{
    QSqlQuery query;
    if(!query.exec(QString("SELECT Model FROM SWM_Task WHERE TaskID = '%1' AND Sequence = '%2'")\
               .arg(id).arg(seq)))
    {
        qWarning()<<query.lastError().text();
        return -1;
    }
    if(query.first())
    {
        int ModelID = query.value("Model").toInt();
        return ModelID;
    }
    return 0;
}

/*
 *  获取任务时间
 */
int SWMTask::getTime(int id, int seq)
{
    QSqlQuery query;
    if(!query.exec(QString("SELECT Time FROM SWM_Task WHERE TaskID = '%1' AND Sequence = '%2'")\
               .arg(id).arg(seq)))
    {
        qWarning()<<query.lastError().text();
        return -1;
    }
    if(query.first())
    {
        int time = query.value("Time").toInt();
        return time;
    }
    return 0;
}

/*
 *  获取任务洗涤剂类型
 */
QString SWMTask::getDetergentType(int id, int seq)
{
    QSqlQuery query;
    if(!query.exec(QString("SELECT DetergentType FROM SWM_Task WHERE TaskID = '%1' AND Sequence = '%2'")\
               .arg(id).arg(seq)))
    {
        qWarning()<<query.lastError().text();
        return "-1";
    }
    if(query.first())
    {
        QString detergentType = query.value("DetergentType").toString();
        return detergentType;
    }
    return "0";
}

/*
 *  获取任务洗涤剂量
 */
QString SWMTask::getDetergentDose(int id, int seq)
{
    QSqlQuery query;
    if(!query.exec(QString("SELECT DetergentDose FROM SWM_Task WHERE TaskID = '%1' AND Sequence = '%2'")\
               .arg(id).arg(seq)))
    {
        qWarning()<<query.lastError().text();
        return "-1";
    }
    if(query.first())
    {
        QString detergentDose = query.value("DetergentDose").toString();
        return detergentDose;
    }
    return "0";
}

/*
 *  获取任务设定温度
 */
int SWMTask::getTemperature(int id, int seq)
{
    QSqlQuery query;
    if(!query.exec(QString("SELECT Temperature FROM SWM_Task WHERE TaskID = '%1' AND Sequence = '%2'")\
               .arg(id).arg(seq)))
    {
        qWarning()<<query.lastError().text();
        return -1;
    }
    if(query.first())
    {
        int temperature = query.value("Temperature").toInt();
        //double temp = temperature/100;
        return temperature;
    }
    return 0;
}

/*
 *  根据状态获取正在执行的流程
 */
int SWMTask::getSequence(int id, QString status)
{
    QSqlQuery query;
    if(!query.exec(QString("SELECT Sequence FROM SWM_Task WHERE TaskID = '%1' AND Status = '%2'")\
               .arg(id).arg(status)))
    {
        qWarning()<<query.lastError().text();
        return -1;
    }
    if(query.first())
    {
        int sequence = query.value("Sequence").toInt();
        return sequence;
    }
    return -2;
}

/*
 *  新建任务的流程
 */
bool SWMTask::createTask(int id, int seq, int model, int time, QString type, QString dose, int temp)
{
    QSqlQuery query;
    if(!query.exec(QString("insert into SWM_Task(TaskID,Sequence,Status,Model,Time,DetergentType,DetergentDose,Temperature) \
                            values('%1','%2', '未执行','%3','%4','%5','%6','%7')")\
               .arg(id).arg(seq).arg(model).arg(time).arg(type).arg(dose).arg(temp)))
    {
        qDebug()<<query.lastError().text();
        return false;
    }
    return true;
}

/*
 *  删除任务的指定流程
 */
bool SWMTask::deleteSequence(int id, int seq)
{
    QSqlQuery query;
    if(!query.exec(QString("DELETE FROM SWM_Task WHERE TaskID = '%1' AND Sequence = '%2'")\
               .arg(id).arg(seq)))
    {
        qWarning()<<query.lastError().text();
        return false;
    }

    if(!query.exec(QString("UPDATE SWM_Task SET Sequence = Sequence-1 WHERE TaskID = '%1' AND Sequence > '%2'")\
               .arg(id).arg(seq)))
    {
        qWarning()<<query.lastError().text();
        return false;
    }
    return true;
}

/*
 *  获取流程的数量
 */
int SWMTask::getSequenceCount(int id)
{
    QSqlQuery query;
    if(!query.exec(QString("SELECT Sequence FROM SWM_Task WHERE TaskID = '%1'")\
               .arg(id)))
    {
        qWarning()<<query.lastError().text();
        return -1;
    }
    int i=0;
    while (query.next()) {
        i++;
    }
    return i;
}

/*
 *  更新任务状态
 */
bool SWMTask::updateStatus(int id, int seq, QString status)
{
    QSqlQuery query;
    QString dataTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");

    if(status == "执行中")
    {
        if(!query.exec(QString("UPDATE SWM_Task SET Status='%3',StartTime='%4' WHERE TaskID='%1' AND Sequence = '%2'")\
                   .arg(id).arg(seq).arg(status).arg(dataTime)))
        {
            qWarning()<<query.lastError().text();
            return false;
        }

    }
    else if(status == "已执行")
    {
        if(!query.exec(QString("UPDATE SWM_Task SET Status='%3',EndTime='%4' WHERE TaskID='%1' AND Sequence = '%2'")\
                   .arg(id).arg(seq).arg(status).arg(dataTime)))
        {
            qWarning()<<query.lastError().text();
            return false;
        }
    }
    else
    {
        if(!query.exec(QString("UPDATE SWM_Task SET Status='%3' WHERE TaskID='%1' AND Sequence = '%2'")\
                   .arg(id).arg(seq).arg(status).arg(dataTime)))
        {
            qWarning()<<query.lastError().text();
            return false;
        }
    }
    return true;
}

/*
 *  更新所有执行中的任务为中断执行
 */
bool SWMTask::updateStatusForOthers()
{
    QSqlQuery query;
    if(!query.exec(QString("UPDATE SWM_Task SET Status = '中断执行' WHERE Status = '执行中' ")))
    {
        qWarning()<<query.lastError().text();
        return false;
    }
    return true;
}

/*
 *  查询是否有下一流程
 */
int SWMTask::hasNextSequence(int id, int seq)
{
    QSqlQuery query;
    if(!query.exec(QString("SELECT * FROM SWM_Task WHERE TaskID = '%1' AND Sequence = '%2'")\
               .arg(id).arg(seq)))
    {
        qWarning()<<query.lastError().text();
        return -1;
    }
    int i=0;
    if(query.first()) {
        i = query.value("Sequence").toInt();
    }
    return i;
}

/*
 *  查询任务所需时间
 */
int SWMTask::getTimeFromId(int id)
{
    QSqlQuery query;
    if(!query.exec(QString("SELECT * FROM SWM_Task WHERE TaskID = '%1'").arg(id)))
    {
        qWarning()<<query.lastError().text();
        return -1;
    }
    int i=0;
    while(query.next()) {
        i += query.value("Time").toInt();
    }
    return i;
}

/*
 *  查询任务各流程所需时间
 */
int SWMTask::getTimeFromIdSeq(int id, int seq)
{
    QSqlQuery query;
    if(!query.exec(QString("SELECT * FROM SWM_Task WHERE TaskID = '%1' AND Sequence = '%2'")\
               .arg(id).arg(seq)))
    {
        qWarning()<<query.lastError().text();
        return -1;
    }
    int i=0;
    if(query.first()) {
        i = query.value("Time").toInt();
    }
    return i;
}

/*
 *  查询剩下任务所需时间
 */
int SWMTask::getOtherTimeFromId(int id)
{
    QSqlQuery query;
    if(!query.exec(QString("SELECT * FROM SWM_Task WHERE TaskID = '%1' AND Status = '未执行'")\
               .arg(id)))
    {
        qWarning()<<query.lastError().text();
        return -1;
    }
    int i=0;
    while(query.next()) {
        i += query.value("Time").toInt();
    }
    return i;
}

/*
 *  查询已执行任务所需时间
 */
int SWMTask::getTimeFromIdDone(int id)
{
    QSqlQuery query;
    if(!query.exec(QString("SELECT * FROM SWM_Task WHERE TaskID = '%1' AND Status = '已执行'")\
               .arg(id)))
    {
        qWarning()<<query.lastError().text();
        return -1;
    }
    int i=0;
    while(query.next()) {
        i += query.value("Time").toInt();
    }
    return i;
}
